﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;

namespace IESN
{
    public class CommonHelper<T> where T:new()
    {
        /// <summary>
        /// 实体类转换成DataTable
        /// </summary>
        /// <param name="modelList">实体类列表</param>
        /// <returns></returns>
        public DataTable FillDataTable(List<T> modelList)
        {
            if (modelList == null || modelList.Count == 0)
            {
                return null;
            }

            //根据实体类得到表结构
            DataTable dt = CreateData(modelList[0]);

            foreach (T model in modelList)
            {
                DataRow dataRow = dt.NewRow();
                foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                {
                    if (propertyInfo.Name == "TableName")
                        continue;
                    dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
                }
                dt.Rows.Add(dataRow);
            }
            return dt;
        }

        /// <summary>
        /// 实体类转换成DataRow
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public DataRow FillDataRow(T model,DataTable dt)
        {
            if (model == null)
            {
                return null;
            }
           
            DataRow dr = dt.NewRow();
            foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
            {
                if (propertyInfo.Name == "TableName")
                    continue;

                dr[propertyInfo.Name] = propertyInfo.GetValue(model, null);
            }
            
            return dr;
        }

        /// <summary>
        /// 行复制
        /// </summary>
        /// <param name="newRow"></param>
        /// <param name="oldRow"></param>
        public void SetDataRow(DataRow sourceRow, DataRow targetRow)
        {
            //  sourceRow.ItemArray ;
            //foreach()
        }

        /// <summary>
        /// 根据实体类得到表结构
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns></returns>
        private DataTable CreateData(T model)
        {
            DataTable dataTable = new DataTable(typeof(T).Name);
            foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
            {
                if (propertyInfo.Name == "TableName")
                    continue;
                dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType));
            }
            return dataTable;
        }

        #region 行数据装对象

        /// <summary>
        /// 填充对象：用DataRow填充实体类
        /// </summary>
        /// <param name="dr">需转换的数据行</param>
        /// <returns>实体类</returns>
        public T FillModel(DataRow dr)
        {
            if (dr == null)
            {
                return default(T);
            }
            //T model = (T)Activator.CreateInstance(typeof(T));  
            T model = new T();
            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);
                if (propertyInfo != null && dr[i] != DBNull.Value)
                    propertyInfo.SetValue(model, dr[i], null);
            }
            return model;
        }

        /// <summary>
        /// 填充对象：用DataTable填充实体类List
        /// </summary>
        /// <param name="dt">要转换的表</param>
        /// <returns>实体类List</returns>
        public List<T> FillModelList(DataTable dt)
        {
            List<T> tList = new List<T>();
            foreach (DataRow dr in dt.Rows)
            {
                tList.Add(FillModel(dr));
            }

            return tList;
        }

        #endregion

    }
}
